<template>
  <div class="app-container">
    <el-form :inline="true" label-width="68px" :model="queryParams">
      <el-form-item label="发布时间">
        <el-date-picker
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="onSearchHandler"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="onDeleteHandler"
        >删除
        </el-button
        >
      </el-col>
    </el-row> -->
    <el-table
      ref="tables"
      :default-sort="defaultSort"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="活动名称" align="center" prop="activityName"/>
      <el-table-column label="社团名称" align="center" prop="societyName"/>
      <el-table-column label="发布人" align="center" prop="applyUserName"/>
      <el-table-column label="审批人" align="center" prop="approvalUserName"/>
      <el-table-column label="经费" align="center" prop="activityFund"/>
      <el-table-column
        label="活动启动时间"
        align="center"
        prop="activityStartTime"
      />
      <el-table-column
        label="活动状态"
        align="center"
        prop="approvalStatus"
        :formatter="statusMethod"
      >
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="applySocirfy(scope.row)"
                     icon="el-icon-view" v-if="scope.row.approvalStatus =='0'">通过
          </el-button
          >
          <el-button type="text" size="mini" @click="rejectSociety1(scope.row)"
                     icon="el-icon-view" v-if="scope.row.approvalStatus =='0'">拒绝
          </el-button
          >
          <el-button
            type="text"
            size="mini"
            @click="onReasonHandler(scope.row)"
            icon="el-icon-view" v-if="scope.row.approvalStatus =='2'">驳回原因
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="onSearchHandler"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form label-width="100px" :model="addlist" ref="addlist">
        <el-row>
          <el-col :span="12">
            <el-form-item label="活动名称">
              <el-input
                placeholder="请输入活动名称"
                v-model="addlist.activityName"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="审批人">
              <el-select
                placeholder="请选择审批人"
                v-model="addlist.approvalUserId"
              >
                <el-option
                  v-for="item in TeacherList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属社团">
              <el-select placeholder="请选择社团" v-model="addlist.societyId">
                <el-option
                  v-for="i in MySocietyInfoList"
                  :key="i.id"
                  :label="i.value"
                  :value="i.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动经费">
              <el-input-number
                controls-position="right"
                type="number"
                size="small"
                placeholder="请输入活动经费"
                v-model="addlist.activityFund"
                :min="0"
              />
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="活动启动时间">
              <el-date-picker
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                format="yyyy 年 MM 月 dd 日"
                value-format="timestamp"
                v-model="addlist.activityStartTime"
                :picker-options="pickerOptions"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col>
          <el-form-item label="活动描述">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入内容"
              v-model="addlist.activityDescribe"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="活动标题">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入内容"
              v-model="addlist.activityTitle"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onAddHandler">确 定</el-button>
        <el-button @click="onCancelHandler">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="Reasontitle"
      :visible.sync="openReason"
      width="500px"
      append-to-body
    >
      <el-input
        v-model="backList.rejectReason"
        type="textarea"
        autosize
        :disabled="Reasontitle != '拒绝原因'"
      ></el-input>
      <el-button style="margin-top: 10px" type="primary" v-if="Reasontitle == '拒绝原因'"
                 @click="rejectSociety()">确 定
      </el-button>
    </el-dialog>
  </div>
</template>

<script>
  import {list, add, remove, auditActivityMethod, rejectSociety, applySocirfy} from "@/api/detail";
  import {queryMySocietyInfoList, queryTeacherList} from "@/api/system/config";

  export default {
    dicts: ["sys_normal_disable"],
    data() {
      return {
        // 默认排序
        defaultSort: {prop: "operTime", order: "descending"},
        // 表格数据
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //新增数据参数
        addlist: {
          //活动描述
          activityDescribe: undefined,
          //活动经费
          activityFund: 0,
          //活动名称
          activityName: undefined,
          //活动开始时间
          activityStartTime: undefined,
          //活动标题
          activityTitle: undefined,
          //审批人ID
          approvalUserId: null,
          //社团id
          societyId: null,
        },
        //社团列表
        MySocietyInfoList: [],
        //审批人列表
        TeacherList: [],
        multiple: true,
        activityId: [],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
          },
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        backTitle: undefined,
        backOpen: false,
        backList: {
          rejectReason: undefined,
        },

        rowId: 0,
        Reasontitle: undefined,
        openReason: false,
      };
    },
    mounted() {
      this.fetchMySocietyInfoList();
      this.fetchTeacherList();
      this.onSearchHandler();
    },
    methods: {
      handleAdd() {
        this.open = true;
        this.title = "新增活动";
      },
      //查询活动
      onSearchHandler() {
        list(this.queryParams).then((res) => {
          this.list = res.rows;
          this.total = res.total;
          console.log(res);
        });
      },
      //关闭弹框
      onCancelHandler() {
        this.open = false;
        this.addlist = {};
      },
      //添加活动
      onAddHandler() {
        this.$modal
        .confirm("活动创建后不可修改，是否添加该活动？")
        .then(() => {
          console.log(this.addlist);
          return add(this.addlist);
        })
        .then(() => {
          this.$modal.msgSuccess("添加成功");
          this.onSearchHandler();
        })
        .finally(() => {
          this.open = false;
          this.addlist = {};
        });
      },
      //     onAddHandler(){
      // this.$refs["addlist"].validate(vaild=>{
      //   if(vaild){
      //     console.log(this.addlist)
      //           return add(this.addlist);

      //   }
      // })

      //     },
      //查询所有社团
      fetchMySocietyInfoList() {
        queryMySocietyInfoList().then((res) => {
          console.log(res.data);
          this.MySocietyInfoList = res.data;
        });
      },
      //查询审批人
      fetchTeacherList() {
        queryTeacherList().then((res) => {
          console.log(res.data);
          this.TeacherList = res.data;
        });
      },
      onDeleteHandler() {
        const activityId = this.activityId;
        this.$modal
        .confirm("是否删除该数据？")
        .then(function () {
          return remove(activityId);
        })
        .then(() => {
          this.onSearchHandler();
          this.$modal.msgSuccess("删除成功");
        });
      },
      handleSelectionChange(selection) {
        this.activityId = selection.map((item) => item.activityId);
        this.multiple = !selection.length;
      },
      statusMethod(row) {
        if (row.approvalStatus == '0') {
          return "未审批";
        } else if (row.approvalStatus == '1') {
          return "已审批";
        } else {
          return "审批未通过";
        }
      },
      onReasonHandler(row) {
        console.log(row);
        this.openReason = true;
        this.Reasontitle = "反驳原因";
        console.log(row.rejectReason);
        this.backList.rejectReason = row.rejectReason;
      },
      rejectSociety1(row) {
        this.backList.rejectReason = "";
        this.backList.activityId = row.activityId;
        this.openReason = true;
        this.Reasontitle = "拒绝原因";
      },
      rejectSociety() {
        rejectSociety(this.backList.activityId, this.backList.rejectReason).then((res) => {
          this.$modal.msgSuccess(res.msg);
          this.openReason = false;
          this.onSearchHandler();
        })

      }, applySocirfy(row) {
        applySocirfy(row.activityId).then((res) => {
          this.$modal.msgSuccess(res.msg);
          this.onSearchHandler();
        })
      }
    },
  };
</script>

<style></style>
